<?php

function authObserver( $event, $poster, $data )
{
	$user = FoundryRegistry::get( 'USER_session' );
	$id = $user->id();

	$controller = $data['controller'];

	if( strlen( $id ) > 10 ) {
		$model = FoundryController::model( 'user' );
		$data = $model->find( '*sess_id = :sid',
			array( ':sid' => $id ) );
	
		if( $data[0]->uid && $user->get( 'isAuthenticated' ) ) {
			define( 'IS_AUTHENTICATED', $user->get( 'isAuthenticated' ) );
			define( 'AUTH_USERID', $data[0]->uid );
			$user->lastActive = time();

			define( 'AUTH_USERNAME', $data[0]->username );
			define( 'AUTH_FULLNAME', $data[0]->fullname );
		
			$role = $data[0]->group_role;
			
			if( $role === '1' ) {
				return true;
			}

			if( ( is_array( $role ) && !empty( $role ) ) ||
				$controller == 'adminMain' ) {
				if( in_array( $controller, $role ) ) {
					return true;
				}
			}
			
			$mods = FoundryController::config( 'modules' );
			$key = $mods['adminMain']['__obKey'];
			
			header( "Location: ".Foundry::config( 'webpath' ).
				'/index.php/'.$key.'/authError' );
			exit;
			
		}
	}	
	header( "Location: ".Foundry::config( 'webpath' ).'/index.php' );
	exit;
}

function verifyAuth()
{
	$user = FoundryRegistry::get( 'USER_session' );
	$id = $user->id();

	if( strlen( $id ) > 10 ) {
		$model = FoundryController::model( 'user' );
		$data = $model->find( '*sess_id = :sid',
			array( ':sid' => $id ) );
	
		if( $data[0]->uid && $user->get( 'isAuthenticated' ) ) {
			define( 'IS_AUTHENTICATED', $user->get( 'isAuthenticated' ) );
			define( 'AUTH_USERID', $data[0]->uid );
			$user->lastActive = time();

			define( 'AUTH_USERNAME', $data[0]->username );
			define( 'AUTH_FULLNAME', $data[0]->fullname );
			
			return true;
		}
	}
	
	return false;
}

?>